package com.easyfun.healthmagicbox.widget;

import com.easyfun.healthmagicbox.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SeekBarWithAdjust extends LinearLayout {

	private int max=100;
	private int progress=50;
	private int incrAmount=1;
	private int decrAmount=-1;
	private SeekBar bar=null;
	
	public SeekBarWithAdjust(final Context ctxt, AttributeSet attrs) {
		super(ctxt, attrs);
		
		this.setOrientation(HORIZONTAL);
		
		TypedArray a=ctxt.obtainStyledAttributes(attrs,R.styleable.Meter);
		
		//获取里面属性用 "名字_ 属性" 连接起来
		progress=a.getInt(R.styleable.Meter_progress, 100);
		max=a.getInt(R.styleable.Meter_max, 100);
		incrAmount=a.getInt(R.styleable.Meter_incr, 1);
		decrAmount=-1*a.getInt(R.styleable.Meter_decr, 1);
		
		a.recycle();
	}
	//当View中所有的子控件 均被映射成xml后触发
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		
//		((Activity)getContext()).getLayoutInflater().inflate(R.layout.meter, this);
		
		LayoutInflater.from(getContext()).inflate(R.layout.seekbarwithadjust, this);
		
		bar=(SeekBar)findViewById(R.id.bar);
		bar.setMax(max);
		bar.setProgress(progress);
		
		Button btn=(Button)findViewById(R.id.incr);
		btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bar.incrementProgressBy(incrAmount);
			}
		});
		
		btn=(Button)findViewById(R.id.decr);
		btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bar.incrementProgressBy(decrAmount);
			}
		});
//		this.addView(view);//不需要add，默认已经add了
	}
	public void setMax(int i) {
		max = i;
		bar.setMax(max);
		
	}
	public void setProgress(int i) {
		progress = i;
		bar.setProgress(progress);
		
	}
	public void setOnSeekBarChangeListener(
			OnSeekBarChangeListener myOnSeekBarChangeListener) {
		bar.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
		
	}
	public int getProgress() {
		return bar.getProgress();
	}

}

